package com.bsit.obu.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtils {
    //时间转毫秒
    public static final Long getMilliSecond(String format, String values) {
        Long time = 0L;
        //得到毫秒数
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            time = sdf.parse(values).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            MyLog.e("转毫秒", "-----异常---");
        }
        return time;
    }

    //毫秒转时间
    public static String getTime(String pattern, String dateTime) {
        String timestr="";
        try {
            Long time = Long.parseLong(dateTime);
            SimpleDateFormat sDateFormat = new SimpleDateFormat(pattern);
            timestr=sDateFormat.format(new Date(time + 0));
        }catch (Exception e) {
            e.printStackTrace();
            MyLog.e("转毫秒", "-----异常---");
        }

        return timestr;
    }
    public static String getLastMonth(String payoffYearMonth) {
        SimpleDateFormat  sd=new    SimpleDateFormat("yyyy-MM");

        try {
            Date  currdate = sd.parse(payoffYearMonth);
            Calendar calendar= Calendar.getInstance();
            calendar.setTime(currdate);
            calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);
            return sd.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void main(String[] args){
        System.out.println(getLastMonth(getTime("yyyy-MM",System.currentTimeMillis()+"")));

    }
}
